package org.devocean.groupbuying.web.backend;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.devocean.groupbuying.domain.Article;
import org.devocean.groupbuying.domain.ArticlesLocalization;
import org.devocean.groupbuying.domain.CategoriesLocalization;
import org.devocean.groupbuying.domain.Media;
import org.devocean.groupbuying.service.ArticlesLocalizationService;
import org.devocean.groupbuying.service.UserService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = { "/gb-cpanel/articlesLocalizations",
		"/gb-cpanel/dealarticlesLocalizations" })
public class ArticlesLocalizationController {

	@Autowired
	private ArticlesLocalizationService articlesLocalizationService;

	@Autowired
	private UserService userService;

	/**
	 * return an Article to edit
	 * 
	 * @param article
	 *            's id
	 * @return article
//	 */
//	@RequestMapping(value = "/{id}/articles/{articleId}", method = RequestMethod.GET)
//	public Map<String, ? extends Object> edit(
//			@PathVariable("articleId") Integer articleId) {
//		try {
//
//			ArticlesLocalization articlesLocalization = articlesLocalizationService
//					.getArticleById(articleId);
//			return renderArticleLocalizationToEdit(articlesLocalization);
//
//		} catch (Exception e) {
//
//			return RenderMapUtils
//					.getModelMapError("Error retrieving Articles from database.");
//		}
//	}
//
//	/**
//	 * Add or update an article
//	 * 
//	 * @param article
//	 * @return
//	 */
//	@RequestMapping(value = "/", method = RequestMethod.POST)
//	public @ResponseBody
//	Map<String, ? extends Object> addOrUpdate(
//			ArticlesLocalization articlesLocalization,
//			String[] _categoriesProxy, String[] articlesMedia,
//			Principal principal) {
//		if (_categoriesProxy != null)
//			for (String cp : _categoriesProxy) {
//				CategoriesLocalization cl = new CategoriesLocalization();
//				cl.setCategoriesLocalizationID(Integer.parseInt(cp));
//				articlesLocalization.getCategoriesLocalizations().add(cl);
//			}
//
//		// Informations about user :
//		articlesLocalization
//				.getArticles()
//				.get(0)
//				.setOwner(
//						userService.getUserById(Integer.valueOf(principal
//								.getName())));
//		//
//
//		if (articlesMedia != null)
//			for (String am : articlesMedia) {
//				Media m = new Media();
//				m.setMediaID(Integer.parseInt(am));
//				articlesLocalization.getMediaLocalizations().add(m);
//			}
//		ArticlesLocalization newArticlesLocalization = articlesLocalizationService
//				.addOrUpdateArticlesLocalization(articlesLocalization);
//		Map<String, Object> data = new ConcurrentHashMap<String, Object>();
//		data.put("id", newArticlesLocalization.getArticlesLocalizationID());
//		data.put("articles[0].id", newArticlesLocalization.getArticles().get(0).getArticleID());
//		data.put("title", newArticlesLocalization.getArticles().get(0).getTitle());
//		data.put("lang", newArticlesLocalization.getArticles().get(0).getLang());
//		return RenderMapUtils.getMap(data);
//
//	}
//
//	@RequestMapping(value = "/localize", method = RequestMethod.GET)
//	public Map<String, ? extends Object> localize(Integer LocalizationKey,
//			String LocalizationRequest) {
//		try {
//			ArticlesLocalization articlesLocalization = articlesLocalizationService
//					.getArticleBylang(LocalizationKey, LocalizationRequest);
//
//			return renderArticleLocalizationToEdit(articlesLocalization);
//
//		} catch (Exception e) {
//			return RenderMapUtils
//					.getModelMapError("Error retrieving Article from database.");
//		}
//
//	}
//
//	public Map<String, ? extends Object> renderArticleLocalizationToEdit(
//			ArticlesLocalization articlesLocalization) {
//		Map<String, Object> data = new ConcurrentHashMap<String, Object>();
//		List<Article> articles = (List<Article>) articlesLocalization
//				.getArticles();
//		Integer[] cl_ids = new Integer[articlesLocalization.getCategoriesLocalizations().size()];
//		
//		
//		data.put("id", articlesLocalization.getArticlesLocalizationID());
//		if (articles != null) {
//			Article article = articles.get(0);
//			data.put("title", article.getTitle());
//			data.put("articles[0].id", article.getArticleID());
//			data.put("articles[0].title", article.getTitle());
//			data.put("articles[0].shorte", article.getShorte());
//			data.put("articles[0].body", article.getBody());
//			data.put("articles[0].lang", article.getLang());
//		}
//		
////		List<Deal> deals = articlesLocalization.getDeals();
////		if (deals.size() > 0) {
////			Deal deal = deals.get(0);
////			SimpleDateFormat simpleFormat = new SimpleDateFormat("MM/dd/yyyy");
////			data.put("deals[0].expireDate",
////					simpleFormat.format(deal.getExpireDate()));
////			data.put("deals[0].tippingPoint", deal.getTippingPoint());
////			data.put("deals[0].maxPurchase", deal.getMaxPurchase());
////			data.put("deals[0].maxPurchasePerClient",
////					deal.getMaxPurchasePerClient());
////			data.put("deals[0].type", deal.getType());
////			data.put("deals[0].status", deal.getStatus());
////			data.put("deals[0].id", deal.getDealID());
////
////		}
//
//		int i = 0;
//		for (CategoriesLocalization categoriesLocalization : articlesLocalization
//				.getCategoriesLocalizations()) {
//			cl_ids[i++] = categoriesLocalization.getCategoriesLocalizationID();
//
//		}
//
//		List<Map<String, Object>> mediaList = new ArrayList<Map<String, Object>>();
//
//		for (Media m : articlesLocalization.getMediaLocalizations()) {
//			Map<String, Object> dataMedia = new ConcurrentHashMap<String, Object>();
//			dataMedia.put("id", m.getMediaID());
//			dataMedia.put("title", m.getTitle());
//			dataMedia.put("media_file_path", m.getMediaFilePath());
//			mediaList.add(dataMedia);
//
//		}
//
//		data.put("_categoriesProxy", cl_ids);
//		data.put("articlesMedia", mediaList);
//		return RenderMapUtils.getMap(data);
//	}

}
